#include "qcoloreditordialog.h"
#include "ui_qcoloreditordialog.h"

#include "../../../libs/shared/qcommonstruct.h"
#include "../../../libs/gradienteditor/qtgradientdialog.h"

#include <QGradient>
#include <QPainter>
#include <QColorDialog>

QColorEditorDialog::QColorEditorDialog(const QVariant value, QWidget *parent):
    QBaseDialog(parent),
    ui(new Ui::QColorEditorDialog),
    m_value(value),
    m_ret(0)
{
    ui->setupUi(this);
    make_color();
}

QColorEditorDialog::~QColorEditorDialog()
{
    delete ui;
}

void QColorEditorDialog::on_okBtn_clicked()
{
    m_ret=1;
    close();
}

void QColorEditorDialog::on_cancelBtn_clicked()
{
    close();
}

void QColorEditorDialog::on_colorBtn_clicked()
{
    QGradient g=m_value.value<QGradient>();
    QColor c=QColorDialog::getColor(g.stops().first().second,this,tr("Set Color"),QColorDialog::ShowAlphaChannel);
    if(c.isValid())
    {
        QLinearGradient l;
        l.setColorAt(0,c);
        l.setColorAt(1,c);
        m_value.setValue<QGradient>(l);

        make_color();
    }
}

void QColorEditorDialog::on_gradientBtn_clicked()
{
    QGradient g=m_value.value<QGradient>();
    bool ok;
    g=QtGradientDialog::getGradient(&ok,g,this);
    if(ok)
    {
        m_value.setValue<QGradient>(g);
        make_color();
    }
}

int QColorEditorDialog::get_ret()
{
    return m_ret;
}

QVariant QColorEditorDialog::get_value()
{
    return m_value;
}

void QColorEditorDialog::make_color()
{

    QGradient g=m_value.value<QGradient>();

    QPixmap pix(ui->color_frame->size());
    pix.fill(Qt::transparent);

    QPainter p(&pix);

    p.fillRect(0,0,ui->color_frame->width(),ui->color_frame->height(),QBrush(g));

    ui->color_frame->setPixmap(pix);
}

void QColorEditorDialog::set_color_type()
{
    ui->gradientBtn->setEnabled(false);
}
